package com.example.wjy;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.room.entity.Team;
import com.example.superyi.myteam.R;
import com.example.wjy.viewmodels.TeamViewModel;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//对队伍进行操作的activity
public class TeamActivity extends AppCompatActivity implements View.OnClickListener {

    String kind;
    Toolbar toolbar;
    Spinner spinner;
    EditText name,numOfAll,introduction;
    String id;
    Button modify,delete,recommend;
    ArrayList<String> kinds = new ArrayList<String>();
    Team team;
    TeamViewModel teamViewModel;
//    //在team中删除小队
//    String url = "http://49.232.60.155:8080/AppServer_war/DeleteTeamServlet";
//
//    //在myTeam中删除小队
//    String url1 = "http://49.232.60.155:8080/AppServer_war/DeleteMyTeamServlet";

    //同时在服务器端删除小队
    String url = "http://49.232.60.155:8080/AppServer_war/TeamServlet";
    //在team中修改小队信息
    String url2 = "http://49.232.60.155:8080/AppServer_war/ModifyTeamServlet";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team);

        teamViewModel = new ViewModelProvider(this).get(TeamViewModel.class);

        kinds.add("数学建模");
        kinds.add("Java");
        kinds.add("C");
        kinds.add("C++");
        kinds.add("全国英语大赛");
        kinds.add("ACM");
        kinds.add("小挑");
        kinds.add("大创");
        kinds.add("蓝桥杯");

        toolbar = findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);
        modify = findViewById(R.id.create);
        modify.setOnClickListener(this);
//        recommend = findViewById(R.id.recommend);
//        delete = findViewById(R.id.delete);
        spinner = findViewById(R.id.spinner2);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                //获取item的内容
                kind = (String) spinner.getItemAtPosition(position);
//                Toast.makeText(CreateTeamActivity.this, kind, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });


        name = findViewById(R.id.ct_TName);
        numOfAll = findViewById(R.id.editTextNumber);
        introduction = findViewById(R.id.editTextTextPersonName);

//        recommend.setOnClickListener(this);
//        delete.setOnClickListener(this);

        Intent intent = getIntent();

        //使用强制类型转换成CharSequence才能对EditText赋值
        CharSequence cs = intent.getStringExtra("name");
        spinner.setSelection(kinds.indexOf(intent.getStringExtra("kind")));
        name.setText(cs);
        cs = intent.getStringExtra("numOfAll");
        numOfAll.setText(cs);
        cs = intent.getStringExtra("introduction");
        introduction.setText(cs);
        id = intent.getStringExtra("id");
        int teamNumOfNow = intent.getIntExtra("teamNumOfNow",1);
        team = new Team("", 0, "", "", "","null",0,teamNumOfNow);
        team.setTeamId(id);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.recommend :

                break;
            case R.id.delete :

                break;
            case R.id.create:
                String teamName = name.getText().toString();
                if("".equals(teamName)) {
                    Toast.makeText(this, "小队名不能为空！", Toast.LENGTH_SHORT).show();
                    break;
                }
                String teamKind = kind;
                String teamNumOfAll = numOfAll.getText().toString();
                if(Integer.parseInt(teamNumOfAll) < team.getTeamNumOfNow()) {
                    Toast.makeText(this, "你的小队人数不合法！", Toast.LENGTH_SHORT).show();
                    break;
                }
                String teamIntro = introduction.getText().toString();
                String teamId = id;


                new Thread(() -> {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("teamId",teamId)
                            .add("teamName",teamName)
                            .add("teamKind",teamKind)
                            .add("teamIntro",teamIntro)
                            .add("teamNumOfAll",teamNumOfAll)
                            .build();
                    Request request = new Request.Builder()
                            .url(url2)
                            .post(requestBody)
                            .build();

                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {

                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        }
                    });
                }).start();



//                Team team1 = new Team(teamName,teamKind,Integer.parseInt(teamNumOfAll),
//                        1,teamIntro,"null");
                Team team1 = new Team(teamId,1,teamKind,teamName,"null",teamIntro,
                        Integer.parseInt(teamNumOfAll),1);
                team1.setTeamId(teamId);
                teamViewModel.modifyTeam(team1);
                this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.team_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.recommend:
                Intent intent = new Intent(TeamActivity.this, RecommendActivity.class);
                startActivity(intent);
//                Toast.makeText(this, "recommend", Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:


                new Thread(() -> {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("teamId",team.getTeamId())
                            .add("flag","2")
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody)
                            .build();

                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {

                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        }
                    });
                }).start();


//
                teamViewModel.deleteTeam(team);
                Toast.makeText(this, "删除成功！", Toast.LENGTH_SHORT).show();
                this.finish();
//                Toast.makeText(this, "delete", Toast.LENGTH_SHORT).show();
                break;
            case R.id.members:

                Intent intent1 = new Intent(TeamActivity.this,MembersActivity.class);
                //将小队id进行，进而查找所有小队成员
                intent1.putExtra("teamId",team.getTeamId());
                startActivity(intent1);

                //发布组队
            case R.id.public_team:
                new Thread(() -> {
                    OkHttpClient client = new OkHttpClient();
                    RequestBody requestBody = new FormBody.Builder()
                            .add("teamId",team.getTeamId())
                            .add("flag","8")
                            .build();
                    Request request = new Request.Builder()
                            .url(url)
                            .post(requestBody)
                            .build();

                    Call call = client.newCall(request);
                    call.enqueue(new Callback() {
                        @Override
                        public void onFailure(@NotNull Call call, @NotNull IOException e) {

                        }

                        @Override
                        public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {

                        }
                    });
                }).start();
                Toast.makeText(this, "发布成功！", Toast.LENGTH_SHORT).show();
                break;






            default:
                break;
        }
        return true;
    }


}